package com.pssmobile.genericglossary.common;

import java.util.Random;
import java.util.Vector;

import com.pssmobile.genericglossary.data.DataAdapter;

public class TermsNavigator implements ITermsNavigator {

    static public TermsNavigator _instance;    
    
    private Vector _listeners = new Vector();
    private Vector _terms = new Vector();
    private int _currentTermIndex;
    
    
    // Hide default constructor
    private TermsNavigator() {
        
    }
    
    private void _notifyCurrentTermChanged( int termID ) {
        for (int i = 0; i < _listeners.size(); ++i) {
            ((ITermsNavigatorListener)_listeners.elementAt(i)).currentTermChanged(termID);
        }
    }
    
    // -------------------------------------------------------------------------
    // PUBLIC FUNCTIONS
    // -------------------------------------------------------------------------

    static public TermsNavigator getInstance() {
        if (null == _instance) {
            _instance = new TermsNavigator();
        }
        return _instance;
    }

    public int loadTerms( String filter ) {
        try {
            _currentTermIndex = 0;
            _terms = DataAdapter.getInstance().findTerms(filter, DataAdapter.SEARCH_TERMS | DataAdapter.SEARCH_DETAILS);
        } catch (Exception e) {
            _currentTermIndex = -1;
            _terms.removeAllElements();
        }
        return _terms.size();
    }
    
    /**
     * Get termID of by index in the list of loaded terms
     * @return termID
     */
    public int getTermIDByIndex(int index) {
        if ((-1 != _currentTermIndex) && (_currentTermIndex < _terms.size())) {
            return ((Integer)_terms.elementAt(index)).intValue();
        }
        return -1;
    }
    
    public int getTermIndexByID( int termID ) {
        int index = -1;
        for (int i = 0; i < _terms.size(); ++i) {
            if (termID == ((Integer)_terms.elementAt(i)).intValue()) {
                index = i;
                break;
            }
        }
        return index;
    }

    /**
     * Get count of terms loaded
     */
    public int size() {
        return _terms.size();
    }    
    
    // -------------------------------------------------------------------------
    // ITermsNavigator IMPLEMENTATION
    // -------------------------------------------------------------------------
    public void addListener(ITermsNavigatorListener listener) {
        if (!_listeners.contains(listener)) {
            _listeners.addElement(listener);
        }
    }
    public void removeListener(ITermsNavigatorListener listener) {
        _listeners.removeElement(listener);
    }

    public int getCurrentTerm() {
        return getTermIDByIndex(_currentTermIndex);
    }
    
    public void setCurrentTerm(int termID) {
        _currentTermIndex = getTermIndexByID(termID);
    }

    public int goNextTerm() {
        if (_terms.size() > 0) {
            if (_currentTermIndex < (_terms.size() - 1)) {
                ++_currentTermIndex;
            } else {
                _currentTermIndex = 0;
            }
            int termID = getTermIDByIndex(_currentTermIndex);
            _notifyCurrentTermChanged(termID);
            return termID;
        }
        return -1;
    }

    public int goPrevTerm() {
        if (_terms.size() > 0) {
            if (_currentTermIndex > 0) {
                --_currentTermIndex;
            } else {
                _currentTermIndex = _terms.size() - 1;
            }
            int termID = getTermIDByIndex(_currentTermIndex);
            _notifyCurrentTermChanged(termID);
            return termID;
        }
        return -1;
    }

    public int goRandomTerm() {
        if (_terms.size() > 0) {
            Random random = new Random(); 
            _currentTermIndex = random.nextInt(_terms.size());
            
            int termID = getTermIDByIndex(_currentTermIndex);
            _notifyCurrentTermChanged(termID);
            return termID;
        }
        return -1;
    }


}
